//
//  AddressPickerView.swift
//  MedicalBeauty
//
//  Created by Henry on 2018/5/9.
//  Copyright © 2018年 shen_h. All rights reserved.
//

import UIKit

typealias BackCityResult = (String,Int,Int)->Void

class AddressPickerView: UIView {

    
    var cityArray:[ProvinceModel]!
    
    var pickerView:UIPickerView!
    
    var selectedProvince = ""
    var seleArea = ""
    var seleCity = ""
    
    var selectedProvinceID = 0
    var seleAreaID = 0
    var seleCityID = 0
    
    var proIndex = 0
    
    
    var cancelClosure:TouchBlock!
    
    var backCityName:BackCityResult!
    
    
    init(frame: CGRect,cityArray:[ProvinceModel]) {
        super.init(frame: frame)
        
        self.cityArray = cityArray
        self.initContent()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initContent(){
//        self.provinces = self.cityArray.allKeys as NSArray
//        self.selectedProvince = self.provinces[0] as! nsdic
//        self.seleCity = (self.cityNames[self.selectedProvince] as! NSArray).firstObject as! String
        let topView = UIView(frame: CGRect(x:0 ,y:0 ,width:self.bounds.size.width ,height:40))
        topView.backgroundColor = UIColor(hexColor: "f0f0f0")
        
        
        let cancelBtn = UIButton(type: .custom)
        cancelBtn.setTitle("取消", for: .normal)
        cancelBtn.setTitleColor(UIColor(hexColor: "333333"), for: .normal)
        cancelBtn.addTarget(self, action: #selector(btnClick(sender:)), for: UIControlEvents.touchUpInside)
        cancelBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        cancelBtn.frame = CGRect(x: 8, y: 0, width: 40, height: 40)
        topView.addSubview(cancelBtn)
        
        let confirm = UIButton(type: .custom)
        confirm.setTitle("确定", for: .normal)
        confirm.setTitleColor(UIColor(hexColor: "333333"), for: .normal)
        confirm.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        confirm.addTarget(self, action: #selector(btnClick(sender:)), for: UIControlEvents.touchUpInside)
        confirm.frame = CGRect(x: self.bounds.size.width - 40 - 8, y: 0, width: 40, height: 40)
        topView.addSubview(confirm)
        self.addSubview(topView)
        
        self.pickerView = UIPickerView.init(frame: CGRect.init(x: 0, y: topView.bottom, width: UIScreen.ScreenWidth, height: self.height - 40 - (Utils.isIphoneX() ? 34 : 0)))
        self.pickerView.delegate = self
        self.pickerView.dataSource = self
        self.pickerView.backgroundColor = UIColor.white
        self.addSubview(self.pickerView)
        
        self.selectedProvince = self.cityArray[0].name
        self.seleArea = self.cityArray[self.proIndex].citys![0].name
        self.seleAreaID = (self.cityArray[self.proIndex].citys![0].areas?[0].id)!
        self.seleCity = (self.cityArray[self.proIndex].citys![0].areas?[0].name)!
        self.seleCityID = self.cityArray[self.proIndex].citys![0].id
        
    }
    
    @objc func btnClick(sender:UIButton){
        if sender.titleLabel?.text == "取消" {
            if self.cancelClosure != nil {
                self.cancelClosure()
            }
        }else {
//            print(self.selectedProvince + self.seleArea + self.seleCity)
            if self.backCityName != nil {
                self.backCityName(self.selectedProvince + self.seleArea + self.seleCity,self.seleAreaID,self.seleCityID)
            }
        }
    }
    
}

extension AddressPickerView : UIPickerViewDelegate,UIPickerViewDataSource {
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 3
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if component == 0 {
            return self.cityArray.count
        }else if component == 1{
            return (self.cityArray[self.proIndex].citys?.count)!
        } else {
            let index = pickerView.selectedRow(inComponent: 1)
            return (self.cityArray[self.proIndex].citys![index].areas?.count)!
        }
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if component == 0 {
            self.proIndex = pickerView.selectedRow(inComponent: 0)
            self.selectedProvince = self.cityArray[row].name
            self.seleArea = self.cityArray[self.proIndex].citys![0].name
            self.seleAreaID = (self.cityArray[self.proIndex].citys![0].areas?[0].id)!
            self.seleCity = (self.cityArray[self.proIndex].citys![0].areas?[0].name)!
            self.seleCityID = self.cityArray[self.proIndex].citys![0].id
            self.pickerView.reloadAllComponents()
        }else if component == 1 {
            self.seleArea = self.cityArray[self.proIndex].citys![row].name
            self.seleAreaID = (self.cityArray[self.proIndex].citys![row].areas?[0].id)!
            self.seleCity = (self.cityArray[self.proIndex].citys![row].areas?[0].name)!
            self.seleCityID = self.cityArray[self.proIndex].citys![row].id
            self.pickerView.reloadAllComponents()
        }else{
            let areasIndex = pickerView.selectedRow(inComponent: 1)
            self.seleCity = (self.cityArray[self.proIndex].citys![areasIndex].areas?[row].name)!
            self.seleAreaID = (self.cityArray[self.proIndex].citys![areasIndex].areas?[row].id)!
        }
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if component == 0 {
            return self.cityArray[row].name
        }else if component == 1 {
            return self.cityArray[self.proIndex].citys![row].name
        } else{
            let areasIndex = pickerView.selectedRow(inComponent: 1)
            return (self.cityArray[self.proIndex].citys![areasIndex].areas?[row].name)!
        }
    }
    
    func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
        return UIScreen.ScreenWidth / 3
    }
    
}




















